package forward.Module.Modules.Render;

import forward.Client;
import forward.api.EventHandler;
import forward.api.Events.Render.EventRender2D;
import forward.api.Value.Mode;
import forward.api.Value.Numbers;
import forward.api.Value.Option;
import forward.Manager.ModuleManager;
import forward.Module.Module;
import forward.Module.ModuleType;
import forward.UI.Font.CFontRenderer;
import forward.UI.Font.FontLoaders;
import forward.Util.Render.ColorUtils;
import forward.Util.Render.RenderUtil;
import java.awt.Color;
import java.util.ArrayList;


import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.resources.I18n;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;

public class HUD extends Module {

	public static int ping=100;
	private static float x;
	private static int y;
	public Numbers<Double> r = new Numbers<Double>("Red", "Red", 47.0, 0.0, 255.0, 1.0);
	public Numbers<Double> g = new Numbers<Double>("Green", "Green", 116.0, 0.0, 255.0, 1.0);
	public Numbers<Double> b = new Numbers<Double>("Blue", "Blue", 253.0, 0.0, 255.0, 1.0);
	public Numbers<Double> alpha = new Numbers<Double>("Alpha", "Alpha", 128.0, 0.0, 255.0, 1.0);
	float red;
	float green;
	float blue;
	private Mode<Enum> mode = new Mode("Mode", "Mode", (Enum[]) Mode1.values(), (Enum) Mode1.Left);
	private Option<Boolean> rainbow = new Option<Boolean>("Rainbow", "Rainbow", false);

	public HUD() {
		super("HUD", new String[] { "gui" }, ModuleType.Render);
		this.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)).getRGB());
		this.setEnabled(true);
		this.setRemoved(true);
		this.addValues(this.mode, this.r, this.g, this.b, this.alpha, this.rainbow);
	}

	@EventHandler
	public void renderHud(EventRender2D event) {
		if (this.mc.gameSettings.showDebugInfo) {
			return;
		}
		CFontRenderer font = FontLoaders.GoogleSans26;
		CFontRenderer font1 = FontLoaders.Comfortaa18;
		String name;
		String direction;
		ArrayList<Module> sorted = new ArrayList<Module>();
		Client.instance.getModuleManager();
		for (Module m : ModuleManager.getModules()) {
			if (!m.isEnabled() || m.wasRemoved())
				continue;
			sorted.add(m);
		}
		sorted.sort((o1,
				o2) -> font1.getStringWidth(
						o2.getSuffix().isEmpty() ? o2.getName() : String.format("%s %s", o2.getName(), o2.getSuffix()))
						- font1.getStringWidth(o1.getSuffix().isEmpty() ? o1.getName()
								: String.format("%s %s", o1.getName(), o1.getSuffix())));
		y = 0;
		for (Module m : sorted) {
			name = m.getSuffix().isEmpty() ? m.getName() : String.format("%s %s", m.getName(), m.getSuffix());
			x = RenderUtil.width() - font1.getStringWidth(name);
			if (Minecraft.getMinecraft().thePlayer!=null) {
				ping = mc.getNetHandler().getPlayerInfo(Minecraft.thePlayer.getUniqueID()).getResponseTime();
			}
			if (this.rainbow.getValue()) {
				red = ColorUtils.getRainbow().getRed() / 255.0f;
				green = ColorUtils.getRainbow().getGreen() / 255.0f;
				blue = ColorUtils.getRainbow().getBlue() / 255.0f;
			} else {
				red = getRed() / 255.0f;
				green = getGreen() / 255.0f;
				blue = getBlue() / 255.0f;
			}
			float a = 255.0f;
			float al = getAlpha() / 255.0f;
			float x1 = 0;
			float x2 = 0;
			float x3 = 0;
			float x4 = 0;
			float x5 = 0;
			if (mode.getValue() == Mode1.Left) {
				x1 = x - 5;
				x2 = RenderUtil.width();
				x3 = x - 8;
				x4 = x - 5;
				x5 = x - 2;
			} else if (mode.getValue() == Mode1.Right) {
				x1 = x - 8;
				x2 = RenderUtil.width() - 3;
				x3 = RenderUtil.width() - 3;
				x4 = RenderUtil.width();
				x5 = x - 5;
			} else if (mode.getValue() == Mode1.None) {
				x1 = x - 5;
				x2 = RenderUtil.width();
				x5 = x - 2;
				a = 0.0f;
			}
			Gui.drawRect(x1, y, x2, y + 10, new Color(0, 0, 0, al).getRGB());
			if (!(mode.getValue() == Mode1.None)) {
				Gui.drawRect(x3, y, x4, y + 10, new Color(red, green, blue).getRGB());
			}
			font1.drawString(name, x5, y + 3, new Color(red, green, blue).getRGB());
			y += 10;
		}
		this.drawPotionStatus(new ScaledResolution(this.mc));
	}

	private void drawPotionStatus(ScaledResolution sr) {
		int y = 0;
		for (PotionEffect effect : this.mc.thePlayer.getActivePotionEffects()) {
			int ychat;
			Potion potion = Potion.potionTypes[effect.getPotionID()];
			String PType = I18n.format(potion.getName(), new Object[0]);
			switch (effect.getAmplifier()) {
			case 1: {
				PType = String.valueOf(PType) + " II";
				break;
			}
			case 2: {
				PType = String.valueOf(PType) + " III";
				break;
			}
			case 3: {
				PType = String.valueOf(PType) + " IV";
				break;
			}
			}
			if (effect.getDuration() < 600 && effect.getDuration() > 300) {
				PType = String.valueOf(PType) + "\u00a77:\u00a76 " + Potion.getDurationString(effect);
			} else if (effect.getDuration() < 300) {
				PType = String.valueOf(PType) + "\u00a77:\u00a7c " + Potion.getDurationString(effect);
			} else if (effect.getDuration() > 600) {
				PType = String.valueOf(PType) + "\u00a77:\u00a77 " + Potion.getDurationString(effect);
			}
			int n = ychat = -10;
			this.mc.fontRendererObj.drawStringWithShadow(PType,
					sr.getScaledWidth() - this.mc.fontRendererObj.getStringWidth(PType) - 2,
					sr.getScaledHeight() - this.mc.fontRendererObj.FONT_HEIGHT + y - 12 - ychat - 20,
					potion.getLiquidColor());
			y -= 10;
		}
	}

	public int getRed() {
		return this.r.getValue().intValue();
	}

	public int getGreen() {
		return this.g.getValue().intValue();
	}

	public int getBlue() {
		return this.b.getValue().intValue();
	}

	public int getAlpha() {
		return this.alpha.getValue().intValue();
	}

	public enum Mode1 {
		Left, Right, None;
	}
}
